package com.chinasoft.movie.controller;

import com.chinasoft.movie.service.IStudentService;
import com.chinasoft.movie.util.KeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/")
public class StudentController extends  BaseController {
    @Autowired
    private ValueOperations redisValue;
    @Autowired
    private IStudentService studentService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @RequestMapping(value = "/insertStudent", method = RequestMethod.GET)
    public  String  insertStudent(HttpServletRequest request){
        return "regStudent";
    }

    @RequestMapping(value = "/insertStudent", method = RequestMethod.POST)
    public  String  insertStudent1(HttpServletRequest request){
//        //System.out.println(getParamMap(request));
//        Map<String,Object> map=getParamMap(request);
//        int count=studentService.insertStudent(map);
//        if(count==1){
//            System.out.println(map);
//            //可能我的redis里面还没有创建学生的集合信息
//            if(redisValue.get("studentList")==null){
//                List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
//                list.add(map);
//                redisValue.set("studentList",list);
//            }else{
//                //redis里面是有这个数据的
//                List<Map<String,Object>> list=(List<Map<String,Object>>)redisValue.get("studentList");
//                list.add(map);
//                redisValue.set("studentList",list);
//
//
//            }
//            //System.out.println("插入成功");
//        }else{
//            //System.out.println("插入失败");
//        }

        //-----------------------------------
        Map<String,Object> map=getParamMap(request);
        //redisValue.set(KeyUtil.getNowYMD(),map);
        String key=new KeyUtil().str;
        //2021-7-22,tb_student,18b85f68-9fb2-4e70-a0c9-0e24b2bc20f7
        //2021-7-22,tb_student,4ee3bba5-d872-4063-8148-1953e18db91a
        //System.out.println(key);
       redisValue.set(key,map);
        //String str="2021-7-22,tb_user-20210722143851476";//数据已经存在于我的redis里面了
        Set<String> keys = redisTemplate.keys("2021-7-22,tb_user-*");//匹配某种key的命名方式
        Iterator<String> it= keys.iterator();
        while(it.hasNext()){
            String key1=it.next();
            System.out.println("key"+key1);
            Map<String,Object> map1=(Map<String,Object>)redisValue.get(key1);
            System.out.println("map1"+map1);
        }
        //key2021-7-22,tb_user-20210722151012474
        //map1{studentName=李四1, studentScore=82, studentClass=JAVA2}

        return "";
    }

    //"2021-7-22,tb_student" K
    //List add <Map<Sring>>  V

}
